﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Ftp.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.SendFile
{
    using System.Net;

    using AlexPilotti.FTPS.Client;
    using AlexPilotti.FTPS.Common;

    using Dhgms.DataManager.Model.ConfigurationElement;
    using Dhgms.TplHelper.Model.Helper;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Task for transferring a file to an FTP server
    /// </summary>
    public class Ftp : Base<RecordProcessor>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Ftp"/> class. 
        /// </summary>
        /// <param name="ftpSettings">
        /// The FTP Settings
        /// </param>
        /// <param name="sourceFullFileName">
        /// File we're tranferring
        /// </param>
        /// <param name="targetFileName">
        /// Name the file will be stored as (will overwrite if it already exists)
        /// </param>
        public Ftp(InternetServer ftpSettings, string sourceFullFileName, string targetFileName)
            : base("Transfer File via FTP")
        {
            this.FtpSettings = ftpSettings;
            this.SourceFullFileName = sourceFullFileName;
            this.TargetFileName = targetFileName;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the FTP Settings
        /// </summary>
        private InternetServer FtpSettings { get; set; }

        /// <summary>
        /// Gets or sets the file we're tranferring
        /// </summary>
        private string SourceFullFileName { get; set; }

        /// <summary>
        /// Gets or sets the name the file will be stored as (will overwrite if it already exists)
        /// </summary>
        private string TargetFileName { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// object that contains result information
        /// </param>
        protected override void OnProcess(RecordProcessor result)
        {
            // ftp the file
            // Get the object used to communicate with the server.
            var ftpsClient = new FTPSClient();

            var credentials = string.IsNullOrWhiteSpace(this.FtpSettings.UserName) ?
                null
                : new NetworkCredential(this.FtpSettings.UserName, this.FtpSettings.Password);

            ftpsClient.Connect(this.FtpSettings.HostName, credentials, ESSLSupportMode.ClearText);
            ftpsClient.SetCurrentDirectory(this.FtpSettings.RequestPath, true);
            ftpsClient.SetTransferMode(ETransferMode.Binary);
            ftpsClient.PutFile(this.SourceFullFileName, this.TargetFileName);

            result.RecordsProcessed = 1;
        }

        #endregion
    }
}